package com.wey.springboot.util;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Arrays;
import java.util.List;

public class CodeGenerator {
  public static void main(String[] args) {
    go(Arrays.asList("task_config"));
  }

  static void go(List<String> tablenames) {
    AutoGenerator mpg = new AutoGenerator();
    // 基础配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setOpen(false);
    gc.setFileOverride(false);
    gc.setServiceImplName("%sService");
    gc.setIdType(IdType.AUTO);
    //  gc.setSwagger2(true);
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setDbType(DbType.MYSQL);
    dsc.setUrl("jdbc:mysql://8.140.96.229:3306/test?serverTimezone=Asia/Shanghai");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("root");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setParent("com.wey");
    pc.setModuleName("springboot");
    pc.setEntity("bean");
    pc.setMapper("mapper");
    pc.setServiceImpl("service");
    pc.setController("unused");
    pc.setService("unused");
    pc.setXml("unused");
    mpg.setPackageInfo(pc);
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    tablenames.forEach(strategy::setInclude);
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    // 是否添加表字段注解
//    strategy.setEntityTableFieldAnnotationEnable(true);
    // 请求路径是否下划线风格
//    strategy.setControllerMappingHyphenStyle(true);
    mpg.setStrategy(strategy);
    mpg.execute();
  }
}
